package com.huike.report.service.impl;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

import com.huike.report.domain.vo.LeadConversionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huike.business.mapper.TbBusinessMapper;
import com.huike.clues.domain.TbAssignRecord;
import com.huike.clues.domain.vo.IndexStatisticsVo;
import com.huike.clues.mapper.SysDeptMapper;
import com.huike.clues.mapper.SysDictDataMapper;
import com.huike.clues.mapper.TbActivityMapper;
import com.huike.clues.mapper.TbAssignRecordMapper;
import com.huike.clues.mapper.TbClueMapper;
import com.huike.common.core.domain.entity.SysDept;
import com.huike.common.utils.SecurityUtils;
import com.huike.contract.mapper.TbContractMapper;
import com.huike.report.domain.vo.IndexVo;
import com.huike.report.mapper.ReportMapper;
import com.huike.report.service.IReportService;

@Service
public class ReportServiceImpl implements IReportService {

    @Autowired
    private TbContractMapper contractMapper;

    @Autowired
    private ReportMapper reportMapper;


    @Autowired
    private SysDictDataMapper sysDictDataMapper;


    @Autowired
    private TbClueMapper clueMapper;

    @Autowired
    private TbActivityMapper activityMapper;

    @Autowired
    private TbBusinessMapper businessMapper;

    @Autowired
    private SysDeptMapper deptMapper;

    @Autowired
    private TbAssignRecordMapper assignRecordMapper;


    @Override
    public IndexVo getIndex(IndexStatisticsVo request) {
        Long deptId= request.getDeptId();
        TbAssignRecord tbAssignRecord=new TbAssignRecord();
        tbAssignRecord.setLatest("1");
        assignRecordMapper.selectAssignRecordList(tbAssignRecord);
        return null;
    }

    @Override
    public List<Map<String, Object>> salesStatisticsForIndex(IndexStatisticsVo request) {
        List<Map<String, Object>> list= contractMapper.contractStatisticsByUser(request);
        for (Map<String, Object> datum : list) {
            Long deptId= (Long) datum.get("dept_id");
            if(deptId!=null){
                SysDept dept= deptMapper.selectDeptById(deptId);
                datum.put("deptName", dept.getDeptName());
            }
        }
        return list;
    }

    private BigDecimal getRadio(Integer all,Long num) {
        if(all.intValue()==0){
            return new BigDecimal(0);
        }
		BigDecimal numBigDecimal = new BigDecimal(num);
		BigDecimal allBigDecimal = new BigDecimal(all);
		BigDecimal divide = numBigDecimal.divide(allBigDecimal,4,BigDecimal.ROUND_HALF_UP);
		return divide.multiply(new BigDecimal(100));
	}

    /**
     * 获取今日所有数据
     * @param beginCreateTime
     * @param endCreateTime
     * @param deptId
     * @return
     */
    @Override
    public IndexVo getTodayInfo(String beginCreateTime,
                                String endCreateTime,
                                String today,
                                Long deptId) {
        IndexVo result = new IndexVo();
        String username = SecurityUtils.getUsername();
        //线索数目
        result.setCluesNum(reportMapper.getCluesNum(beginCreateTime,endCreateTime,username));
        //商机数目
        result.setBusinessNum(reportMapper.getBusinessNum(beginCreateTime,endCreateTime,username));
        //合同数目
        result.setContractNum(reportMapper.getContractNum(beginCreateTime,endCreateTime,username));
        //销售金额
        if (reportMapper.getSalesAmount(beginCreateTime,endCreateTime,username)!= null){
            result.setSalesAmount(reportMapper.getSalesAmount(beginCreateTime,endCreateTime,username));
        }
        //今日线索数目
        result.setTodayCluesNum(reportMapper.getTodayCluesNum(today,username));
        //今日商机数目
        result.setTodayBusinessNum(reportMapper.getTodayBusinessNum(today,username));
        //今日合同数目
        result.setTodayContractNum(reportMapper.getTodayContractNum(today,username));
        //今日销售金额
        if(reportMapper.getTodaySalesAmount(today,username)!=null){
            result.setTodaySalesAmount(reportMapper.getTodaySalesAmount(today,username));
        }
        //待跟进线索数目
        result.setTofollowedCluesNum(reportMapper.getTofollowedCluesNum(beginCreateTime,endCreateTime,username));
        //待跟进商机数目
        result.setTofollowedBusinessNum(reportMapper.getTofollowedBusinessNum(beginCreateTime,endCreateTime,username));
        //待分配线索数目
        result.setToallocatedCluesNum(reportMapper.getToallocatedCluesNum(beginCreateTime,endCreateTime,username));
        //待分配商机数目
        result.setToallocatedBusinessNum(reportMapper.getToallocatedBusinessNum(beginCreateTime,endCreateTime,username));

        return result;
    }



    /**
     * 线索转化龙虎榜
     */
    @Override
    public List<LeadConversionVo> getLeadConversion(String beginCreateTime, String endCreateTime) {
        ArrayList<LeadConversionVo> leadConversionVos = new ArrayList<>();

        List<String> leadConversionName = reportMapper.getLeadConversionName(beginCreateTime, endCreateTime);
        for (String name : leadConversionName) {
            LeadConversionVo leadConversionVo = new LeadConversionVo();
            leadConversionVo.setCreate_by(name);
            leadConversionVo.setDeptName(reportMapper.getUserNameAtDept(name));
            //查询线索转化个数
            leadConversionVo.setNum(reportMapper.getLeadConversionNum(beginCreateTime, endCreateTime,name));
            //线索转化个数/线索数量=转化率
            if ((100.00*reportMapper.getLeadConversionNum(beginCreateTime, endCreateTime,name)
                    /  reportMapper.getLeadNum(beginCreateTime, endCreateTime,name))>0){
                leadConversionVo.setRadio(100.00*reportMapper.getLeadConversionNum(beginCreateTime, endCreateTime,name)
                        /  reportMapper.getLeadNum(beginCreateTime, endCreateTime,name));
            }
            leadConversionVos.add(leadConversionVo);
        }
        return leadConversionVos;
    }

}